package com.hejielife.lock;

/**
 * 2019年8月1日 下午3:08:48
 * 
 * @author hejielife
 * @despribe string锁对象的改变问题失效
 */
public class ChangeLock {

	private String lock = "lock";

	private void method() {
		synchronized (lock) {
			try {
				System.out.println("当前线程 : " + Thread.currentThread().getName() + "开始");
				// string锁改变功能失效，打开注释可以尝试
				// lock = "change lock";
				Thread.sleep(2000);
				System.out.println("当前线程 : " + Thread.currentThread().getName() + "结束");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {

		final ChangeLock changeLock = new ChangeLock();
		Thread t1 = new Thread(new Runnable() {
			public void run() {
				changeLock.method();
			}
		}, "t1");
		Thread t2 = new Thread(new Runnable() {
			public void run() {
				changeLock.method();
			}
		}, "t2");
		t1.start();
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		t2.start();
	}

}
